package com.jftone.bh.app.util;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.TmpImg;
import org.jftone.component.BeanContext;
import org.jftone.service.BaseService;
import org.jftone.util.DateUtil;

import java.io.File;

public class UploadFile {
	
	public static String TEMP_DIRECTORY = "";
	
	public static String FILE_TYPE = "png,jpg,gif,jpeg,mp4";

	public File makeDirectory()  {
		File tempDir = new File(TEMP_DIRECTORY);
		if(!tempDir.exists()){
			tempDir.mkdirs();
		}
		return tempDir;
	}
	
	public int getTmpImgId(short imgType, String imgSrc) throws Exception{
		TmpImg tmpImg = new TmpImg();
		tmpImg.setImgType(imgType);
		tmpImg.setImgSrc(imgSrc);
		tmpImg.setTmpId(0);
		tmpImg.setRemoveTag(SysConst.FILE_REMOVE_TAG_NO);
		tmpImg.setUpdateTime(DateUtil.getMillisecond());
		tmpImg = BeanContext.getBean(BaseService.class).save(tmpImg);
		return tmpImg.getId();
	}

	public enum State {  
        OK(0, "上传成功"),  
        ERROR(1, "上传失败"),  
        OVER_FILE_LIMIT(2, "超过上传大小限制"),  
        NO_SUPPORT_EXTENSION(3, "不支持的扩展名"),
        NOT_LOGIN(4, "没有登录");
  
        private int code;  
        private String message;  
        private State(int code, String message) {  
            this.code = code;  
            this.message = message;  
        }  
  
        public int getCode() {  
            return code;  
        }  
        public String getMessage() {  
            return message;  
        } 
    }  
	
	public enum FileDir{  
		BASE(1, "base"),
        NEWS(2, "news"),
        APP(3, "app"),
        MOMENT(4, "moment");
        private int imgType; 
        private String dir;  
        private FileDir(int imgType, String dir) {   
            this.imgType = imgType;
            this.dir = dir;
        }  
        public int getImgType() {  
            return imgType;  
        }  
        public String getDir() {  
            return dir;  
        }
    } 
}
